// pages/tabs/category/category.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '', // 搜索关键词
    categories: [], // 分类列表
    currentCategoryId: '', // 当前选中的分类ID
    currentCategory: null, // 当前选中的分类信息
    products: [], // 当前分类下的商品列表
    allProducts: [] // 所有商品数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadCategoryData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 页面显示时刷新数据
    this.loadCategoryData();
  },

  /**
   * 加载分类数据
   */
  loadCategoryData() {
    // 模拟分类数据，实际项目中应该从后端API获取
    const mockCategories = [
      {
        id: '1',
        name: '粮油调料',
        icon: '/assets/images/category-grain.svg',
        count: 15
      },
      {
        id: '2', 
        name: '新鲜蔬菜',
        icon: '/assets/images/category-vegetable.svg',
        count: 28
      },
      {
        id: '3',
        name: '时令水果',
        icon: '/assets/images/category-fruit.svg',
        count: 22
      },
      {
        id: '4',
        name: '肉禽蛋奶',
        icon: '/assets/images/category-meat.svg',
        count: 18
      },
      {
        id: '5',
        name: '海鲜水产',
        icon: '/assets/images/category-seafood.svg',
        count: 12
      },
      {
        id: '6',
        name: '休闲零食',
        icon: '/assets/images/category-snack.svg',
        count: 35
      }
    ];

    // 模拟商品数据
    const mockProducts = [
      {
        id: '1',
        categoryId: '1',
        name: '东北五常大米',
        image: '/assets/images/rice.svg',
        price: '89.90',
        originalPrice: '99.90',
        isHot: true,
        tags: ['有机', '新鲜']
      },
      {
        id: '2',
        categoryId: '1',
        name: '金龙鱼调和油',
        image: '/assets/images/oil.svg',
        price: '45.80',
        originalPrice: null,
        isHot: false,
        tags: ['健康']
      },
      {
        id: '3',
        categoryId: '2',
        name: '有机蔬菜组合',
        image: '/assets/images/vegetables.svg',
        price: '35.60',
        originalPrice: '42.00',
        isHot: true,
        tags: ['有机', '新鲜']
      },
      {
        id: '4',
        categoryId: '3',
        name: '精选水果礼盒',
        image: '/assets/images/fruits.svg',
        price: '128.00',
        originalPrice: '158.00',
        isHot: false,
        tags: ['礼盒', '精选']
      }
    ];

    this.setData({
      categories: mockCategories,
      allProducts: mockProducts,
      currentCategoryId: mockCategories[0]?.id || '',
      currentCategory: mockCategories[0] || null
    });

    // 加载第一个分类的商品
    if (mockCategories.length > 0) {
      this.loadProductsByCategory(mockCategories[0].id);
    }
  },

  /**
   * 根据分类ID加载商品
   */
  loadProductsByCategory(categoryId) {
    const products = this.data.allProducts.filter(product => product.categoryId === categoryId);
    this.setData({
      products
    });
  },

  /**
   * 分类点击事件
   */
  onCategoryTap(e) {
    const categoryId = e.currentTarget.dataset.id;
    const category = this.data.categories.find(cat => cat.id === categoryId);
    
    this.setData({
      currentCategoryId: categoryId,
      currentCategory: category
    });

    this.loadProductsByCategory(categoryId);
  },

  /**
   * 商品点击事件
   */
  onProductTap(e) {
    const product = e.currentTarget.dataset.product;
    
    // 跳转到商品详情页
    wx.navigateTo({
      url: `/pages/product/detail?id=${product.id}`
    });
  },

  /**
   * 搜索提交
   */
  onSearch(e) {
    const searchValue = e.detail.value;
    this.setData({
      searchValue
    });

    if (searchValue.trim()) {
      this.searchProducts(searchValue);
    } else {
      // 清空搜索时恢复当前分类商品
      this.loadProductsByCategory(this.data.currentCategoryId);
    }
  },

  /**
   * 搜索清空
   */
  onSearchClear() {
    this.setData({
      searchValue: ''
    });
    // 恢复当前分类商品
    this.loadProductsByCategory(this.data.currentCategoryId);
  },

  /**
   * 搜索商品
   */
  searchProducts(keyword) {
    const filteredProducts = this.data.allProducts.filter(product => 
      product.name.toLowerCase().includes(keyword.toLowerCase()) ||
      (product.tags && product.tags.some(tag => tag.includes(keyword)))
    );
    
    this.setData({
      products: filteredProducts
    });
  },

  /**
   * 去首页
   */
  onGoHome() {
    wx.switchTab({
      url: '/pages/tabs/home/home'
    });
  },

  /**
   * 刷新数据
   */
  onRefresh() {
    this.loadCategoryData();
    wx.showToast({
      title: '刷新成功',
      icon: 'success'
    });
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.loadCategoryData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 可以在这里实现分页加载更多商品
    console.log('触底加载更多');
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '商品分类',
      path: '/pages/tabs/category/category'
    };
  }
})